//
//  VC12Gesture.swift
//  ChapterAll
//
//  Created by Kevin on 2020/5/2.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class VC12Gesture: UIViewController ,UIGestureRecognizerDelegate{
    
    lazy var uiView = { ()->UIView in
        let uiX:CGFloat = 80
        let uiW = view.frame.width - uiX * 2
        let uiY = (view.frame.height - uiW) * 0.5
        let ui = UIView(frame: CGRect(x: uiX, y: uiY, width: uiW, height: uiW))
        ui.backgroundColor = UIColor.red
        ui.layer.allowsEdgeAntialiasing = true
        return ui
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(uiView)
        
        click7()
    }
    
    func click7() -> Void {
        let pin = UIPinchGestureRecognizer(target: self, action: #selector(click7Pin(gesture:)))
        pin.delegate = self
        uiView.addGestureRecognizer(pin)
        
        let rotation = UIRotationGestureRecognizer(target: self, action: #selector(click7Rotation(gesture:)))
        uiView.addGestureRecognizer(rotation)
    }
    
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }
    
    @objc func click7Rotation(gesture:UIRotationGestureRecognizer) -> Void {
        uiView.transform3D = CATransform3DRotate(uiView.transform3D, gesture.rotation, 0, 0, 1)
        gesture.rotation = 0
    }
    
    @objc func click7Pin(gesture:UIPinchGestureRecognizer) -> Void {
        uiView.transform3D = CATransform3DScale(uiView.transform3D, gesture.scale, gesture.scale, 0)
        gesture.scale = 1
    }
    
    func click6() -> Void {
        let gesture = UIPanGestureRecognizer(target: self, action: #selector(clickUIPanGestureRecognizer(gesture:)))
        uiView.addGestureRecognizer(gesture)
    }
    
    @objc func clickUIPanGestureRecognizer(gesture:UIPanGestureRecognizer) -> Void {
        let point = gesture.translation(in: uiView)
        uiView.transform3D = CATransform3DTranslate(uiView.transform3D, point.x, point.y, 0)
        gesture.setTranslation(CGPoint.zero, in: uiView)
    }
    
    func click5() -> Void {
        let gesture = UIPinchGestureRecognizer(target: self, action: #selector(clickUIPinchGestureRecognizer(gesture:)))
        uiView.addGestureRecognizer(gesture)
    }
    
    @objc func clickUIPinchGestureRecognizer(gesture:UIPinchGestureRecognizer) -> Void {
        print("scale=\(gesture.scale)")
        
        // func 1
        // uiView.transform = CGAffineTransform(scaleX: gesture.scale, y: gesture.scale)
        
        // func 2
        // uiView.transform3D = CATransform3DScale(uiView.transform3D, gesture.scale, gesture.scale, 0)
        // gesture.scale = 1
        
        // func 3
        uiView.transform3D = CATransform3DMakeScale(gesture.scale, gesture.scale, 0)
    }
    
    func click4() -> Void {
        let gesture = UIRotationGestureRecognizer(target: self, action: #selector(clickUIRotationGestureRecognizer(gesture:)))
        uiView.addGestureRecognizer(gesture)
    }
    
    @objc func clickUIRotationGestureRecognizer(gesture:UIRotationGestureRecognizer) -> Void {
        print("rotation=\(gesture.rotation)")
        
        // func 1
        // uiView.transform = CGAffineTransform(rotationAngle: gesture.rotation)
        
        // func 2
        // uiView.transform3D = CATransform3DRotate(uiView.transform3D, gesture.rotation, 0, 0, 1)
        // gesture.rotation = 0
        
        // func 3
        uiView.transform3D = CATransform3DMakeRotation(gesture.rotation, 0, 0, 1)
    }
    
    func click3() -> Void {
        // 方向，默认从左到右
        let gesture1 = UISwipeGestureRecognizer(target: self, action: #selector(clickUISwipeGestureRecognizer(gesture:)))
        uiView.addGestureRecognizer(gesture1)
        
        // 方向，从右到左
        let gesture2 = UISwipeGestureRecognizer(target: self, action: #selector(clickUISwipeGestureRecognizer(gesture:)))
        gesture2.direction = .left
        uiView.addGestureRecognizer(gesture2)
    }
    
    @objc func clickUISwipeGestureRecognizer(gesture:UISwipeGestureRecognizer) -> Void {
        if gesture.direction == .left {
            print("从右到左")
        }else if gesture.direction == .right {
            print("从左到右")
        }
    }
    
    func click2() -> Void {
        let gesture = UILongPressGestureRecognizer(target: self, action: #selector(clickUILongPressGestureRecognizer(gesture:)))
        // 误差范围
        gesture.allowableMovement = 10
        // 最低长按时间
        gesture.minimumPressDuration = 1
        uiView.addGestureRecognizer(gesture)
    }
    
    @objc func clickUILongPressGestureRecognizer(gesture:UILongPressGestureRecognizer) -> Void {
        // 防止重调两次
        if gesture.state != .began {
            return
        }
        let allowable = gesture.allowableMovement
        let miniDuration = gesture.minimumPressDuration
        print("allowable=\(allowable),miniDuration=\(miniDuration)")
    }
    
    func click1() -> Void {
        let gesture = UITapGestureRecognizer(target: self, action: #selector(clickUITapGestureRecognizer(gesture:)))
        // 点击次数
        gesture.numberOfTapsRequired = 2
        // 要求手指个数
        gesture.numberOfTouchesRequired = 2
        uiView.addGestureRecognizer(gesture)
    }
    
    @objc func clickUITapGestureRecognizer(gesture:UITapGestureRecognizer) -> Void {
        let tapRequired = gesture.numberOfTapsRequired
        let touchesRequired = gesture.numberOfTouchesRequired
        print("tapRequired=\(tapRequired),touchesRequired=\(touchesRequired)")
    }
}
